/*
 * Created: Author: Aléx Carvalho / Date: Feb 10, 2015
 * 
 * Changed: $Author: ac280593@gmail.com $ / $Date: 2015-05-13 13:23:31 +0000 (Wed, 13 May 2015) $
 */
package com.cpm.entities.id;

import com.cpm.context.SystemLogger;
import com.cpm.entities.Identity;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Objects;
import javax.persistence.Embeddable;

@Embeddable
public interface Id<TYPE> extends Serializable {

    public TYPE getId();

    default boolean equalsAllFields(Object identity) {

        if (identity instanceof Identity) {

            boolean equals = true;
            try {
                for (Field f : identity.getClass().getDeclaredFields()) {

                    Field fi = this.getClass().getDeclaredField(f.getName());
                    fi.setAccessible(true);
                    f.setAccessible(true);
                    if (Objects.equals(f.get(identity), fi.get(this))) {
                        equals = equals && true;
                    }
                    else {
                        equals = false;
                    }
                }

                return equals;
            }
            catch (SecurityException | NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
                SystemLogger.logException(e);
            }
        }

        return false;
    }
}
